﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class Manage_Modle_CateGory_Xml2DB : System.Web.UI.Page
{

    string xmppath = @"E:\dcms 2.1\Dcms.Web\webmenu.xml";
    XmlDocument doc;
    XmlDocument urls;
    string language = "cn";
    DataSet CateDs = new DataSet();
    ArrayList CateAl = new ArrayList();
    protected void Page_Load(object sender, EventArgs e)
    {
        xmppath = Server.MapPath("/") + @"\webmenu.xml";
        GetUrlXml();
        ToDB();
    }
    //加载xml
    private void loadXMl()
    {
        try
        {
            doc = new XmlDocument();
             
            doc.Load(xmppath);
         
        }
        catch
        {
            return;
        }

    }
    /// <summary>
    /// 取得url列表
    /// </summary>
    private void GetUrlXml()
    {
        try
        {
            urls = new XmlDocument();
            urls.Load(Server.MapPath(@"../ModelField/FilePath.xml"));
        }
        catch
        {
            return;
        }
    }


    //读取xml数据并储存到数据库
    private void ToDB()
    {
        loadXMl();
        if (doc != null)
        {

            //delModel();
            XmlNode root = doc.GetElementsByTagName("root")[0];
            language = root.FirstChild.Attributes["language"].Value;
            Dcms.BLL.Dcms_CateBLL newCateBLL = new Dcms.BLL.Dcms_CateBLL();
            //if (newCateBLL.DelAll() == true)
            //{
               CateDs= newCateBLL.GetList("");
                forXML(root.FirstChild, newCateBLL.GetMaxId());
            //}

                foreach (DataRow dr in CateDs.Tables[0].Rows)
                {
                    int drint=0;
                    foreach (object o in CateAl)
                    {
                        if (dr["Cate_Guid"].ToString() == o.ToString())
                        {
                            drint = 1;
                        }
                    }
                    if (drint == 0)
                    {
                        newCateBLL.DelGuid(dr["Cate_Guid"].ToString());
                    }
                }
        }
    }
    /// <summary>
    /// 删除模块类型表中所有非基础模块
    /// </summary>
    private void delModel()
    {

        Dcms.BLL.Dcms_ModelBLL ModelBLL = new Dcms.BLL.Dcms_ModelBLL();
        ModelBLL.DelNewModel();
    }
    //递归遍历
    private void forXML(XmlNode xn, int ParentID)
    {
        int cateorder = 0;
        foreach (XmlNode node in xn.ChildNodes)
        {

            Dcms.BLL.Dcms_CateBLL newCateBLL = new Dcms.BLL.Dcms_CateBLL();
            Dcms.Model.Dcms_CateModel newCateModel = new Dcms.Model.Dcms_CateModel();
            Dcms.Model.Dcms_CateModel parent = new Dcms.Model.Dcms_CateModel();
            ///循环 NewCate list判断guid是否存在，不存在的话就添加，存在的话，修改
            int forint = 0;
            foreach (DataRow dr in CateDs.Tables[0].Rows)
            {
                if (dr["Cate_Guid"].ToString() == node.Attributes["Guid"].Value)
                {
                    forint = 1;
                }
            }
            if (forint == 1)
            {
               newCateModel= newCateBLL.GetModelByGuid(node.Attributes["Guid"].Value); 
            }

            parent = newCateBLL.GetModelByID(ParentID);
            newCateModel.Cate_Key = node.Attributes["ModelKey"].Value;
            newCateModel.Cate_Name = node.Attributes["name"].Value;

            newCateModel.Cate_Order = cateorder;
            string type = node.Attributes["type"].Value;
            //是否为新建分类
            if (type.Split('|')[1].Split('#').Length < 2)
            {
                newCateModel.Cate_Flag = type.Split('|')[1];
                newCateModel.Cate_ModelKeyId = 0;
                //Dcms.BLL.Dcms_ModelBLL ModelBLL = new Dcms.BLL.Dcms_ModelBLL();
                //newCateModel.Cate_Url = ModelBLL.GetUrl(type.Split('|')[1], 0);
               

            }
            else
            {
                newCateModel.Cate_Flag = type.Split('|')[1].Split('#')[0];
               
               // newCateModel.Cate_ModelKeyId = 1;

                Dcms.BLL.Dcms_ModelBLL ModelBLL=new Dcms.BLL.Dcms_ModelBLL();
                int ModelKeyId = ModelBLL.CheckModel(type.Split('|')[0], type.Split('|')[1].Split('#')[0],language);
                //查找model表，如果不存在，添加数据
                if ( ModelKeyId> 0)
                {
                    //取得模块的url给分类
                    newCateModel.Cate_ModelKeyId=ModelKeyId;
                  //  newCateModel.Cate_Order = ModelBLL.GetOrder(type.Split('|')[0], type.Split('|')[1].Split('#')[0], language);
                   // newCateModel.Cate_Url = ModelBLL.GetUrl(type.Split('|')[1].Split('#')[0], newCateModel.Cate_ModelKeyId);
                }
                else
                {
                    ///新模块类型写入模块
                    Dcms.Model.Dcms_ModelModel DcModel = new Dcms.Model.Dcms_ModelModel();
                    //int i = ModelBLL.GetNewOrder(type.Split('|')[1].Split('#')[0], language);
                    //DcModel.Model_Order = i;
                    DcModel.Model_Level = 0;
                    DcModel.Model_Name = type.Split('|')[0];
                    DcModel.Model_Type = type.Split('|')[1].Split('#')[0];
                    DcModel.Model_Lang = language;
                    DcModel.Model_UseFlag = 1;
                   // newCateModel.Cate_Order=ModelBLL.AddModel(DcModel).Model_Order;

                    newCateModel.Cate_ModelKeyId = ModelBLL.AddModel(DcModel).Model_Order;
                    //newCateModel.Cate_Url = DcModel.Model_Url;
                }


            }
            newCateModel.Cate_Lang = language;
            
        
           
            if (node.Attributes["magstate"].Value != null)
            {
                newCateModel.Cate_magstate = Convert.ToInt16(node.Attributes["magstate"].Value);
            }
            else
            {
                newCateModel.Cate_magstate = 0;
            }

            if (node.Attributes["showtype"].Value != null)
            {
                newCateModel.Cate_showtype = Convert.ToInt16(node.Attributes["showtype"].Value);
            }
            else
            {
                newCateModel.Cate_showtype = 0;
            }

            if (node.Attributes["browse"].Value != null)
            {
                newCateModel.Cate_browse = Convert.ToInt16(node.Attributes["browse"].Value);
            }
            else
            {
                newCateModel.Cate_browse = 0;
            }

                                                                                                                                                                                   
            if (node.Attributes["auditing"].Value != null)
            {
                newCateModel.Cate_auditing= Convert.ToInt16(node.Attributes["auditing"].Value);
            }
            else
            {
                newCateModel.Cate_auditing = 0;
            }
            newCateModel.Cate_Guid = node.Attributes["Guid"].Value;
           
          
          
            string showtype="";
             if(newCateModel.Cate_showtype==0)
             {
                   showtype="edit";
             }
              else if(newCateModel.Cate_showtype==1)
            {
                    showtype="list";
            }
            foreach (XmlNode no in urls.GetElementsByTagName("model"))
            {


                if (String.Compare(no.Attributes["name"].Value,newCateModel.Cate_Flag,true)==0&&String.Compare(no.Attributes["PageType"].Value,showtype,true)==0)
                {
                    newCateModel.Cate_ViewURL = string.Format(no.Attributes["DestPath"].Value.Substring(9,no.Attributes["DestPath"].Value.Length-9), newCateModel.Cate_ModelKeyId);
                    break;
                }
            }


            newCateModel.Cate_IsChild = 1;
            ///如果guid存在修改，如果不存在就添加;
            if (forint == 1)
            {
                newCateBLL.GuidUpdate(newCateModel);
              
            }
            else
            {
                newCateModel.Cate_OpType = "EDIT";
                newCateModel.Cate_AllAction = "Read|Add|Edit|Delete";
                if (parent != null)
                {
                    newCateModel.Cate_Layer = parent.Cate_Layer + 1;
                }
                else
                {
                    newCateModel.Cate_Layer = 0;
                }
                if (parent != null)
                {
                    newCateModel.Cate_IDPath = parent.Cate_IDPath;
                }
                else
                {
                    newCateModel.Cate_IDPath = "";
                }
                newCateModel.Cate_State = "1";
                newCateModel.Cate_AddTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
                newCateModel.Cate_ParentID = ParentID;
                newCateBLL.AddModel(newCateModel);
               
            }
            //把XML中的GUID保存到arraylis中
            CateAl.Add(node.Attributes["Guid"].Value);


            forXML(node, newCateBLL.GetMaxId());
            cateorder++;
        }
    }
}
